package com.neu.his.user.controller;

import java.util.Arrays;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neu.his.commonutils.JwtUtils;
import com.neu.his.commonutils.R;
import com.neu.his.user.entity.User;
import com.neu.his.user.service.UserService;

/**
 * 
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/his/user")
public class UserController {
	@Autowired
	private UserService userService;

	/*
	 * 分页查询 支持根据科室id 挂号级别id 用户名进行查询
	 */
	@GetMapping("/getUserList")
	public R getUserList(int page, int limit, String keywords, String keywords02, String keywords03) {
		page = (page - 1) * limit;
		int total = userService.getUserCount(keywords, keywords02, keywords03);
		List<User> list = userService.getUserList(page, limit, keywords, keywords02, keywords03);
		return R.ok().data("list", list).data("total",total);

	}

//	添加用户的方法
	@PostMapping("/addUser")
	public R addUser(@RequestBody User user) {
		boolean save = userService.addUser(user);
		if (save) {
			return R.ok().message("添加成功");
		} else {
			return R.error().message("添加失败");
		}
	}

//	修改用户的方法
	@PostMapping("/updateUser")
	public R updateUser(@RequestBody User user) {
		boolean update = userService.updateUser(user);
		if (update) {
			return R.ok().message("修改成功");
		} else {
			return R.error().message("修改失败");
		}
	}

	/*
	 * 登录返回token
	 */
	@GetMapping("/getUserBean")
	public R getUserBean02(String keywords, String keywords2) {
		User user = userService.getUserBean(keywords, keywords2);
		String token = JwtUtils.getJwtToken(user.getId(), user.getUserName());
		return R.ok().data("list", user).data("token", token);

	}

	/**
	 * 批量删除根据多个id
	 */

	@PostMapping("/deleteUserByIds")
	public R deleteUserByIds(@RequestBody Integer[] ids, HttpServletResponse response) {
		boolean del = userService.deleteUserByIds(ids);
		if (del) {
			return R.ok().message("删除成功");
		} else {
			return R.error().message("删除失败");
		}

	}
}
